<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>药品类别管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/admin.css">

</head>
<style>
    .edit-form {
        text-align: center;
        margin-top: 25px;
    }

    .edit-form label {
        font-size: 16px;
        margin-left: 0;
    }

    .edit-form .edit-form-input {
        position: relative;
        right: 35px;
    }

</style>
<body>
<form style="display: none" class="layui-form" id="edit-input">
    <div class="edit-form">
        <div class="layui-form-item layui-input-inline edit-form-input">
            <label class="layui-form-label">名称:</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="edit-typeName" autocomplete="off" id="edit-typeName" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item layui-input-inline edit-form-input">
            <label class="layui-form-label">描述:</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="edit-dest" id="edit-dest" autocomplete="off" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item layui-input-inline">
            <div class="layui-input-inline">
                <button type="button" class="layui-btn edit-btn">提交</button>
            </div>
        </div>
    </div>
</form>
<div class="layui-form-item" style="margin:0 auto;width:300px;">
    <div class="layui-input-inline">
        <input type="text" class="layui-input" id="query-input" placeholder="查询药品类别">
    </div>
    <a class="layui-btn" id="query" data-type="reload">查询</a>
</div>
<div class="query-table">

    <div style="text-align: left">
        <button type="button" class="layui-btn" id="delete">删除</button>
        <button type="button" class="layui-btn" id="add">添加</button>
    </div>
</div>
<table id="medicine-type" lay-filter="demo"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['form', 'table', 'laypage', 'layer'], function () {
        var table = layui.table,
            layer = layui.layer,
            $ = layui.jquery;
        var indexEditLayer = 0;
        var indexAdd = 0;
        var editId = 0;
        var gObj;
        var editOrAdd = 0  // 1是add 2是edit
        var key = $("#query-input").value;
        var checkboxes = new Array();
        layer.load(2);
        /*
         * 表格
        * */
        setTimeout(function () {
            table.render({
                elem: '#medicine-type'
                , url: '/select.medicineType'
                // ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , height: 500
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'id', title: '类别ID', sort: true, align: "center"}
                    , {field: 'typeName', title: '类别名称', align: "center"}
                    , {field: 'date', title: '创建时间', sort: true, align: "center"}
                    , {field: 'dest', title: '描述', align: "center"}
                    , {field: 'right', title: "操作", align: 'center', toolbar: '#barDemo'}
                ]]
                , page: true //开启分页
                , id: "reload"
            });
            layer.closeAll('loading');
        }, 500);

        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            gObj = obj;
            // var layEvent = obj.event;
            var tr = obj.tr;
            var id = obj.data.id;
            editId = id;
            if (obj.event === 'edit') {
                editOrAdd = 2; //编辑模式
                indexEditLayer = layer.open({
                    type: 1
                    // ,shade : true
                    , title: '编辑药品类别'
                    , content: $("#edit-input")
                    , area: ['400px', '250px']
                    , cancel: function () {
                        $("#edit-typeName").val("");
                        $("#edit-dest").val("");
                    }
                });
                // console.log(obj);
                // console.log("typeName:"+obj.data.typeName);
                $("#edit-typeName").val(obj.data.typeName);
                $("#edit-dest").val(obj.data.dest);
            } else if (obj.event === 'del') {
                console.log('sss');
                layer.confirm('真的要删除吗', function (index) {
                    // obj.del();
                    // $.get("/delete.medicineType", {id: id});
                    $.ajax("/delete.medicineType", {
                        type: "post",
                        data: {
                            id: id
                        },
                        success: function (data) {
                            var json;
                            if (typeof  data == "string") json = JSON.parse(data);
                            else json = data;
                            if (json.code == 1) {
                                layer.msg("无权限")
                            }
                            else {
                                obj.del();
                            }
                            table.reload('reload', {
                                url: '/select.medicineType'
                            });
                        }
                    });
                    layer.close(index);
                })
            }
        });
        //监听复选框，获取已选复选框的id
        table.on('checkbox(demo)', function (obj) {
            while (checkboxes.length != 0) {
                checkboxes.pop();
            }
            $("input:checkbox:checked").each(function () {
                var id = $(this).parent().parent().next().children("div").text();
                if (!isNaN(id)) {
                    checkboxes.push(id);
                }
                // console.log(id);
                // console.log(isNaN(id));
            })
        })
        $(".edit-btn").click(function () {
            var typeName = $("#edit-typeName").val();
            var dest = $("#edit-dest").val()
            // console.log("typeName:"+$("#typeName").val());
            if (editOrAdd === 1) {
                $.ajax("/add.medicineType", {
                    type: "post",
                    data: {
                        typeName: typeName,
                        dest: dest
                    },
                    success: function (data) {
                        var json;
                        if (typeof  data == "string") json = JSON.parse(data);
                        else json = data;
                        if (json.code == 1) {
                            layer.msg("无权限")
                        } else {
                            layer.msg("添加成功");
                            table.reload('reload', {
                                url: '/select.medicineType'
                            });
                        }
                    },
                });
            } else if (editOrAdd === 2) {
                $.ajax("/edit.medicineType", {
                    type: "post",
                    data: {
                        typeName: typeName,
                        dest: dest,
                        id: editId
                    },
                    success: function (data) {
                        debugger;
                        var json;
                        if (typeof  data == "string") json = JSON.parse(data);
                        else json = data;
                        if (json.code == 1) {
                            layer.msg("无权限")
                        } else {
                            if (data.isEdit == true) {
                                gObj.update({
                                    typeName: typeName
                                    , dest: dest
                                });
                            }
                        }
                    },
                });
            }
            //输入框清空
            $("#edit-typeName").val("");
            $("#edit-dest").val("");
            layer.close(indexEditLayer);
            return false;
        });

        $("#add").click(function () {
            editOrAdd = 1; //添加模式
            indexEditLayer = layer.open({
                type: 1
                , title: '添加药品类别'
                , content: $("#edit-input")
                , area: ['400px', '250px']
            });
        });
        $("#delete").click(function () {
            this.blur();
            layer.confirm('真的要删除吗', {
                success: function (layro, index) {
                    $(document).on('keydown', layro, function (e) {
                        if (e.keyCode == 13) {
                            layro.find('a.layui-layer-btn0').trigger('click');
                        }
                    })
                }
                , yes: function (index) {
                    layer.close(index);
                    for (var i = 0; i < checkboxes.length; i++) {
                        $.get("/delete.medicineType", {id: checkboxes[i]});
                    }
                    layer.msg("删除成功");
                    table.reload('reload', {
                        url: '/select.medicineType'
                    });
                }
            });
        });


        var active = {
            reload: function () {
                var queryInput = $("#query-input");
                table.reload('reload', {
                    url: '/selectByKey.medicineType'
                    , page: {
                        curr: 1
                    }
                    , where: {
                        key: queryInput.val()
                    }
                });
            }
        }
        $("#query").click(function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        })
    });
</script>

</body>
</html>
